﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YTSK.Model;
using System.Xml;

namespace YTSK.cSK.BW
{
    class cMakeXML_DZP
    {
        private string _FHR;
        private string _SKR;
        private string _fpdz;
        private string _yh;
        private string _version;
        public cMakeXML_DZP()
        {
            _FHR = cSK.cSKSet.loadsave(SkConfig.复核人);
            _SKR = cSK.cSKSet.loadsave(SkConfig.收款人);
            _fpdz = cSK.cSKSet.loadsave(SkConfig.默认发票地址电话);
            _yh = cSK.cSKSet.loadsave(SkConfig.默认银行及账号);
            _version = cSK.cSKSet.loadsave(SkConfig.税控分类版本号);
            if (_version.Length == 0) { throw new Exception("请先设置税控设备参数"); }
        }
        public string MakeXML(string savefile, Customer customer, Rdrecord rd, List<Dictionary<string, object>> l_rds)
        {
            var xml = new XmlDocument();
            var declare = xml.CreateXmlDeclaration("1.0", "GBK", null);
            xml.AppendChild(declare);
            #region 表头定义
            var kp = xml.CreateElement("business");
            var attr = xml.CreateAttribute("id");
            attr.Value = "FPKJ";
            kp.Attributes.Append(attr);
            attr = xml.CreateAttribute("comment");
            attr.Value = "发票开具";
            kp.Attributes.Append(attr);

            var REQUEST_COMMON_FPKJ = xml.CreateElement("REQUEST_COMMON_FPKJ");
            attr = xml.CreateAttribute("class");
            attr.Value = "REQUEST_COMMON_FPKJ";
            var topmain = xml.CreateElement("COMMON_FPKJ_FPT");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_FPT";
            topmain.Attributes.Append(attr);
            #endregion

            #region 表头栏目
            var top = xml.CreateElement("FPQQLSH");
            top.InnerText = DateTime.Now.ToString("yyMMddhhmmss");
            topmain.AppendChild(top);
            top = xml.CreateElement("KPLX");
            top.InnerText = "0";
            topmain.AppendChild(top);
            top = xml.CreateElement("BMB_BBH");
            top.InnerText = "17.0";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_NSRSBH");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_MC");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_DZDH");
            top.InnerText = _fpdz;
            topmain.AppendChild(top);
            top = xml.CreateElement("XSF_YHZH");
            top.InnerText = _yh;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_NSRSBH");
            top.InnerText = rd.TaxNo;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_MC");
            top.InnerText = customer.cCusName;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_DZDH");
            top.InnerText = rd.Address + " " + rd.Phone;
            topmain.AppendChild(top);
            top = xml.CreateElement("GMF_YHZH");
            top.InnerText = rd.Bank + " " + rd.BankAccount;
            topmain.AppendChild(top);
            top = xml.CreateElement("KPR");
            top.InnerText = "开票人";
            topmain.AppendChild(top);
            top = xml.CreateElement("SKR");
            top.InnerText = _SKR;
            topmain.AppendChild(top);
            top = xml.CreateElement("FHR");
            top.InnerText = _FHR;
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_DM");
            top.InnerText = "";
            topmain.AppendChild(top);
            top = xml.CreateElement("YFP_HM");
            top.InnerText = "";
            topmain.AppendChild(top);
            #endregion

            #region 明细开票
            var main = xml.CreateElement("COMMON_FPKJ_XMXXS");
            attr = xml.CreateAttribute("class");
            attr.Value = "COMMON_FPKJ_XMXX";
            main.Attributes.Append(attr);
            attr = xml.CreateAttribute("size");
            attr.Value = "1";
            main.Attributes.Append(attr);
            decimal sumjshj = 0;
            decimal sumje = 0;
            decimal sumse = 0;

            int irowcount = 1;
            for (int i = 0; i < l_rds.Count; i++)
            {
                var tori = l_rds[i];
                var t = tori.ToObj<Rdrecords>();
                if (t.cInvName.ObjToString().Length > 0)
                {
                    var d = xml.CreateElement("COMMON_FPKJ_XMXX");
                    var cTaxType = tori["cTaxType"].ObjToString();
                    if (cTaxType.Length != 19)
                    { throw new Exception(string.Format("产品[{0}]的税收编码[{1}]不是是19位", t.cInvName, cTaxType)); }
                    var detail = xml.CreateElement("SPBM");
                    detail.InnerText = cTaxType;
                    d.AppendChild(detail);

                    detail = xml.CreateElement("ZXBM");
                    detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("YHZCBS");
                    detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("ZZSTSGL");
                    detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("LSLBS");
                    detail.InnerText = "";
                    d.AppendChild(detail);
                    detail = xml.CreateElement("FPHXZ");
                    detail.InnerText = cTaxType;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("XMMC");
                    detail.InnerText = t.cInvName;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("GGXH");
                    detail.InnerText = t.cInvStd;
                    d.AppendChild(detail);
                    detail = xml.CreateElement("DW");
                    detail.InnerText = t.cDW;
                    d.AppendChild(detail);
                    var jehs = t.iSumSale.ObjToDecimal();
                    var taxrate = t.iTaxRateSale.ObjToDecimal();
                    var jeori = jehs / (1 + taxrate / 100);
                    decimal jeinput = Math.Round(jeori, 15, MidpointRounding.AwayFromZero);
                    //for (int m = 15; m <= 25; m++)
                    //{
                    //    var tv = Math.Round(jeori, m, MidpointRounding.AwayFromZero);
                    //    var tv1 = Math.Round(tv * (1 + taxrate / 100), 2, MidpointRounding.AwayFromZero);
                    //    if (tv1 == jehs)
                    //    {
                    //        jeinput = tv;
                    //        break;
                    //    }
                    //    else
                    //    {
                    //        if (m == 25) { jeinput = tv; }
                    //    }
                    //}                    
                    var se = jehs - jeinput;
                    var sl = t.iQuantity;
                    var dj = jeinput / sl;
                    var itaxrate = Math.Round(t.iTaxRateSale.ObjToDecimal() / 100, 2);
                    sumjshj += jehs;
                    sumje += jeinput;
                    sumse += se;
                    detail = xml.CreateElement("XMSL");
                    detail.InnerText = sl.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("XMDJ");
                    detail.InnerText = dj.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("XMHJ");
                    detail.InnerText = jehs.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("XMJE");
                    detail.InnerText = jeinput.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("SL");
                    detail.InnerText = itaxrate.ObjToString();
                    d.AppendChild(detail);
                    detail = xml.CreateElement("SE");
                    detail.InnerText = se.ObjToString();
                    d.AppendChild(detail);

                    main.AppendChild(d);
                    irowcount++;
                }
            }
            #endregion

            #region 表头项目增加
            top = xml.CreateElement("JSHJ");
            top.InnerText = sumjshj.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJJE");
            top.InnerText = sumje.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("HJSE");
            top.InnerText = sumse.ToString();
            topmain.AppendChild(top);
            top = xml.CreateElement("BZ");
            top.InnerText = "";
            topmain.AppendChild(top);
            #endregion

            #region 完成XML文件
            REQUEST_COMMON_FPKJ.AppendChild(topmain);
            REQUEST_COMMON_FPKJ.AppendChild(main);
            kp.AppendChild(REQUEST_COMMON_FPKJ);
            xml.AppendChild(kp);
            #endregion

            #region 保存文件
            if (System.IO.File.Exists(savefile))
            { System.IO.File.Delete(savefile); }
            xml.Save(savefile);
            return savefile;
            #endregion

        }



    }
}
